﻿using Sc.Domain;
using Sc.Persistence;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Service
{
    public class DriverCertificateService
    {
        private readonly DriverCertificateDAO dal = new DriverCertificateDAO();
		
		#region  BasicMethod
		/// <summary>
		/// 增加一条数据
		/// </summary>
        public int Add(DriverCertificate model, IList<Sys_Attachments> list)
		{
            var tag = 0;
            if (model.Id == 0)
            {
                tag = dal.Add(model);
            }
            else
            {
                tag = model.Id;
                dal.Update(model);
            }
            if (tag > 0 && list.Count > 0)
                new Sys_AttachmentsDAO().SaveAttachments(list, tag);
            return tag;
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(DriverCertificate model)
		{
			return dal.Update(model);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			
			return dal.Delete(Id);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			return dal.DeleteList(Idlist );
		}
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public DriverCertificate GetModel(int id)
		{
            var ent = dal.GetModel(id);
            ent.AttactList = new Sys_AttachmentsDAO().GetList("DCFE", id);
            return ent;
		}
		#endregion  BasicMethod
        #region UserMethod
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="orgz">机构ID</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="pageIndex">当前第几页</param>
        /// <param name="baseItemId">分类</param>
        /// <param name="userName">司机姓名</param>
        /// <returns></returns>
        public CommModel<DriverCertificate> GetPageData(int orgz, int pageSize, int pageIndex, int baseItemId ,string userName)
        {
            string where = string.Empty;
            if (orgz > 0)
            {
                where = "AND OrgzNo=" + orgz;
            }
            if (baseItemId>0)
            {
                where += " AND BaseItemId=" + baseItemId;
            }
            if (!string.IsNullOrEmpty(userName))
            {
                where += " AND Name Like '%" + userName + "'";
            }
            Hashtable param = new Hashtable();
            param.Add("A1", pageIndex);
            param.Add("A2", pageSize);
            param.Add("A3", where);
            param.Add("A4", "Vw_DriverCertificates");
            param.Add("A5", "CreateTime DESC");
            var ht = CommDAO.CommPagination(param);
            DataTable dt = ht["data"] as DataTable;
            List<DriverCertificate> list = new List<DriverCertificate>();
            foreach (DataRow item in dt.Rows)
            {
                list.Add(dal.DataRowToModel(item));
            }
            CommModel<DriverCertificate> ent = new CommModel<DriverCertificate>();
            ent.Data = list;
            ent.PageCount = Convert.ToInt32(ht["PageCount"]);
            ent.RecordCount = Convert.ToInt32(ht["record"]);
            return ent;
        }
        #endregion
    }
}
